<?php
/**
 * Minhlog 错误功能控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Controller
 * @package    Frontend
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: ErrorController.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class ErrorController extends Minh_Controller_Action
{
    public function init()
    {
        $this->view = Zend_Registry::get('view');
    }

    public function errorAction()
    {
        // 从错误请求中获取对象
        $errors = $this->_getParam('error_handler');

        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
            // 404错误 (控制器或者动作错误)
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->message = '您现在浏览的网页可能已被删除、重命名或暂时不可用';
                $this->view->code  = 404;
                // 找不到控制器处理
                if ($errors->type == Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER) {
                    $this->view->info = sprintf(
                            '在模块 "%s" 中找不到 "%s" 控制器,请联系网站管理员!',
                            $errors->request->getModuleName(),
                            $errors->request->getControllerName()
                    );
                }
                // 找不到动作处理
                if ($errors->type == Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION) {
                    $this->view->info = sprintf(
                            '在模块 "%s" 中的 "%s" 控制器里找不到 "%s" 视图,请联系网站管理员!',
                            $errors->request->getModuleName(),
                            $errors->request->getControllerName(),
                            $errors->request->getActionName()
                    );
                }
                break;
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
            default:
            // 代码执行错误
                $this->getResponse()->setHttpResponseCode(500);
                $this->view->message = '应用程序出错';
                $this->view->code  = 500;
                $this->view->info  = $errors->exception;
                break;
        }

        /**
         * @todo 网站错误通知邮件
         */

        // 获取应用环境
        $this->view->env = $this->getInvokeArg('env');

        // 获取当前请求
        $this->view->request = $errors->request;

        // 捕获程序异常
        $this->view->exception = $errors->exception;

        echo $this->view->render('error.phtml');
    }
}